package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.EquipDao;
import com.jbp.exception.RRException;
import com.jbp.model.Equip;
import com.jbp.model.EquipBag;
import com.jbp.model.EquipRoom;
import com.jbp.model.Room;
import com.jbp.service.EquipBagService;
import com.jbp.service.EquipRoomService;
import com.jbp.service.EquipService;
import com.jbp.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.util.List;
import java.util.Map;

/**
 * 装备(Equip)表服务实现类
 * @author cc
 * @since 2020-08-31 18:43:09
 */
@Service
public class EquipServiceImpl extends ServiceImpl<EquipDao, Equip> implements EquipService {
    @Autowired
    private EquipRoomService equipRoomService;

    @Autowired
    private EquipBagService equipBagService;

    @Autowired
    private RoomService roomService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Equip> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null){
            queryWrapper.like("id",params.get("id"));
        }
        IPage<Equip> page = this.page(
                new Query<Equip>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<Equip> roomBgList(Integer roomId) {
        Room room = roomService.getById(roomId);
        if (room==null) {
            throw new RRException("房间不存在");
        }
        QueryWrapper<Equip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", "room");
        queryWrapper.eq("type", "bg");
        queryWrapper.eq("status", "normal");
        queryWrapper.orderByAsc("sort");

        List<Equip> list = list(queryWrapper);
        for (Equip equip : list) {
            //如果不是免费的 查看是否有权限
            if (equip.getFree()==0) {
                EquipRoom equipRoom = equipRoomService.getByEquip(roomId, equip.getId());
                equip.setFree(equipRoom==null ? 0 : 1);
            }
        }
        return list;
    }

    //个人装扮列表
    @Override
    public List<Equip> equipList(String type) {
        QueryWrapper<Equip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", "usr");
        queryWrapper.eq("type", type);
        queryWrapper.eq("status", "normal");
        queryWrapper.orderByAsc("sort");

        return list(queryWrapper);
    }
}